using System.Collections.Generic;
using UnityEngine;

public class SplineVector3
{
	public class SplinePoint
	{
		public float time;

		public Vector3 value;
	}

	public List<SplinePoint> points = new List<SplinePoint>();

	public bool isDeltaTimeBetweenAllSplinePointsConstant;

	public int pointsCount { get; protected set; }

	public void Clear()
	{
		pointsCount = 0;
	}

	public void Add(Vector3 value, float time)
	{
		pointsCount++;
		if (pointsCount >= points.Count)
		{
			points.Add(new SplinePoint());
		}
		SplinePoint splinePoint = points[pointsCount - 1];
		splinePoint.time = time;
		splinePoint.value = value;
	}

	protected int ClosestIndexBeforeTime(float time)
	{
		if (isDeltaTimeBetweenAllSplinePointsConstant)
		{
			float num = points[1].time - points[0].time;
			int value = Mathf.FloorToInt((time - points[0].time) / num);
			return Mathf.Clamp(value, 0, pointsCount - 1);
		}
		for (int i = 0; i < pointsCount; i++)
		{
			if (points[i].time > time)
			{
				return i - 1;
			}
		}
		return pointsCount - 1;
	}

	private int Bounds(int index)
	{
		return Mathf.Clamp(index, 0, pointsCount - 1);
	}

	public Vector3 ValueAtTime(float time)
	{
		int num = ClosestIndexBeforeTime(time);
		SplinePoint splinePoint = points[Bounds(num - 1)];
		SplinePoint splinePoint2 = points[Bounds(num)];
		SplinePoint splinePoint3 = points[Bounds(num + 1)];
		SplinePoint splinePoint4 = points[Bounds(num + 2)];
		float t = Mathf.InverseLerp(splinePoint2.time, splinePoint3.time, time);
		return Vector3Ex.CatmullRomLerp(splinePoint.value, splinePoint2.value, splinePoint3.value, splinePoint4.value, t);
	}
}
